package org.example.spring_trans.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.spring_trans.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;



@Slf4j
@RequestMapping("/trans")
@RestController
public class TransactionalController {
    @Autowired
    private UserService userService;
//    @Transactional
    @RequestMapping("/r1")
    public String registry(String name,String password){

       Integer result =userService.registryUser(name,password);
        if (result==1){
            log.info("注册成功,更新行数",result);
        }
        int a=10/0;
        return "注册成功";

    }
    @Transactional
    @RequestMapping("/r2")
    public String r2(String name,String password){

        Integer result =userService.registryUser(name,password);
        if (result==1){
            log.info("注册成功,更新行数",result);
        }
        int a=10/0;
        return "注册成功";

    }
    @Transactional
    @RequestMapping("/r3")
    public String r3(String name,String password){

        Integer result =userService.registryUser(name,password);
        if (result==1){
            log.info("注册成功,更新行数",result);
        }
        try {
            int a = 10 / 0;
        }catch (Exception e){
            log.error("异常",e);
        }
        return "注册成功";

    }
    @Transactional
    @RequestMapping("/r4")
    public String r4(String name,String password){

        Integer result =userService.registryUser(name,password);
        if (result==1){
            log.info("注册成功,更新行数",result);
        }
        try {
            int a = 10 / 0;
        }catch (Exception e){
            log.error("异常",e);
            throw e;
        }
        return "注册成功";

    }
    @Transactional
    @RequestMapping("/r5")
    public String r5(String name,String password){

        Integer result =userService.registryUser(name,password);
        if (result==1){
            log.info("注册成功,更新行数",result);
        }
        try {
            int a = 10 / 0;
        }catch (Exception e){
            log.error("异常",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";

    }
    @Transactional
    @RequestMapping("/r6")
    public String r6(String name,String password)throws IOException {

        Integer result =userService.registryUser(name,password);
        if (result==1){
            log.info("注册成功,更新行数",result);
        }
        if(true) {
            throw new IOException();
        }
        return "注册成功";

    }
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r7")
    public String r7(String name,String password)throws IOException {

        Integer result =userService.registryUser(name,password);
        if (result==1){
            log.info("注册成功,更新行数",result);
        }
        if(true) {
            throw new IOException();
        }
        return "注册成功";

    }
    @Transactional(isolation = Isolation.DEFAULT)
    @RequestMapping("/r8")
    public String r8(String name,String password)throws IOException {

        Integer result =userService.registryUser(name,password);
        if (result==1){
            log.info("注册成功,更新行数",result);
        }
        if(true) {
            throw new IOException();
        }
        return "注册成功";

    }
}
